package com.exe.rege.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exe.rege.common.R;
import com.exe.rege.entity.CategoryEntity;
import com.exe.rege.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 分类管理
* */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 增加分类信息
     * @param categoryEntity
     * @return
     */
    @PostMapping
    public R<CategoryEntity> save(@RequestBody CategoryEntity categoryEntity){
        log.info("新增分类名称{}", categoryEntity.getName());
        //数据库里已经对名字做了限制，故此可以不判断是否重复
        categoryService.save(categoryEntity);
        return R.success(categoryEntity);
    }

    /**
     * 删除分类
     * 注意：如果该分类关联了某个套餐或者菜品便不能直接删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类id{}", ids);
//        categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("删除分类成功");
    }

    /**
     * 修改分类信息
     * @param categoryEntity
     * @return
     */
    @PutMapping
    public R<CategoryEntity> update(@RequestBody CategoryEntity categoryEntity){
        log.info("更新的分类信息为{}", categoryEntity.toString());
        categoryService.updateById(categoryEntity);
        return R.success(categoryEntity);
    }

    /**
     * 分页查询分类
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page = {}, pageSize = {}, name = {}",page,pageSize,name);

        //构造分页构造器
        Page<CategoryEntity> pageInfo = new Page(page,pageSize);
        //构造条件分页构造器
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件过滤，如果name为null就不能用like，此处没有查
        //queryWrapper.like(StringUtils.isNotEmpty("name"), CategoryEntity::getName, name);
        queryWrapper.orderByAsc(CategoryEntity::getSort);
        //执行查询
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 按条件查询分类数据
     * @param categoryEntity
     * @return
     */
    @GetMapping("/list")
    public R<List<CategoryEntity>> list(CategoryEntity categoryEntity){
        //条件构造器
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();

        //添加条件
        //先判断类型是否为空，不为空则比较类型是什么
        queryWrapper.eq(categoryEntity.getType() != null, CategoryEntity::getType, categoryEntity.getType());

        //添加排序条件
        //先按照 排序 排，再按照更新时间排
        queryWrapper.orderByAsc(CategoryEntity::getSort).orderByDesc(CategoryEntity::getUpdateTime);

        List<CategoryEntity> list = categoryService.list(queryWrapper);

        return R.success(list);
    }


}
